<template>
  <div class="login-container">
    <!-- 左侧3D城市景观 -->
    <div class="login-left" :style="{ backgroundImage: `url(${randomBg})` }"></div>

    <!-- 右侧登录表单 -->
    <div class="login-right">
      <div class="login-form">
        <!-- 标题 -->
        <div class="login-title">
          账号登录
        </div>

        <!-- 登录表单 -->
        <el-form 
          ref="loginFormRef" 
          :model="loginForm" 
          :rules="rules" 
          class="form-content"
        >
          <!-- 账号输入框 -->
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="账号"
              clearable
              prefix-icon="el-icon-user"
              class="input-large"
            ></el-input>
          </el-form-item>

          <!-- 密码输入框 -->
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              placeholder="密码"
              show-password
              clearable
              prefix-icon="el-icon-lock"
              class="input-large"
            ></el-input>
          </el-form-item>

          <!-- 登录按钮 -->
          <el-button 
            type="primary" 
            class="login-button"
            @click="handleLogin"
            :loading="loading"
          >
            登录
          </el-button>
        </el-form>

        <!-- 版权信息 -->
        <div class="copyright">
          Powered By 长远软件工作室
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'

// 背景图片数组
const bgArr = [
  'https://sxjygl.byxy.com/img/images/pc_bg_1.jpg',
  'https://sxjygl.byxy.com/img/images/pc_bg_2.jpg',
  'https://sxjygl.byxy.com/img/images/pc_bg_3.jpg',
  'https://sxjygl.byxy.com/img/images/pc_bg_4.jpg',
  'https://sxjygl.byxy.com/img/images/pc_bg_5.webp',
  'https://sxjygl.byxy.com/img/images/pc_bg_6.webp',
  'https://sxjygl.byxy.com/img/images/phone_bg_1.jpg',
  'https://sxjygl.byxy.com/img/images/phone_bg_2.jpg',
  'https://sxjygl.byxy.com/img/images/phone_bg_3.jpg',
  'https://sxjygl.byxy.com/img/images/phone_bg_4.jpg',
  'https://sxjygl.byxy.com/img/images/phone_bg_5.png',
  'https://sxjygl.byxy.com/img/images/phone_bg_6.webp'
]

// 随机选择一张背景图
const randomBg = computed(() => {
  const randomIndex = Math.floor(Math.random() * bgArr.length)
  return bgArr[randomIndex]
})

const loginFormRef = ref(null)
const loginForm = ref({
  username: 'SMF202205599',
  password: '••••••••••••',
})

const rules = ref({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
})

const loading = ref(false)

const handleLogin = () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      // 这里添加登录逻辑
      setTimeout(() => {
        loading.value = false
        ElMessage.success('登录成功')
        // 登录成功后跳转到主页
        // router.push('/')
      }, 1000)
    }
  })
}
</script>

<style lang="less" scoped>
.login-container {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.login-left {
  flex: 1;
  background-size: cover;
  background-position: center;
  position: relative;
  overflow: hidden;
}

.login-right {
  width: 400px;
  background-color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: -2px 0 10px rgba(0, 0, 0, 0.1);

  .login-form {
    width: 90%;
    max-width: 350px;
  }

  .login-title {
    text-align: center;
    font-size: 24px;
    font-weight: bold;
    margin-bottom: 30px;
    color: #303133;
  }

  .form-content {
    margin-top: 20px;
  }

  .input-large {
    height: 50px;
    font-size: 16px;
  }

  .login-button {
    width: 100%;
    margin-top: 10px;
    background-color: #0078ff;
    border: none;
    border-radius: 4px;
    height: 50px;
    font-size: 18px;
  }

  .copyright {
    text-align: center;
    margin-top: 40px;
    color: #909399;
    font-size: 16px;
  }
}

// 响应式设计
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }

  .login-left {
    display: none;
  }

  .login-right {
    width: 100%;
    box-shadow: none;
  }

  .login-form {
    padding: 20px;
  }

  .login-title {
    font-size: 20px;
    margin-bottom: 20px;
  }

  .input-large {
    height: 45px;
    font-size: 14px;
  }

  .login-button {
    height: 45px;
    font-size: 16px;
  }

  .copyright {
    font-size: 14px;
    margin-top: 20px;
  }
}
</style>